package com.blb.community.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.community.entity.SysRole;
import com.blb.community.entity.SysRoleMenu;
import com.blb.community.entity.vo.RoleMenuVO;
import com.blb.community.entity.vo.RoleVO;
import com.blb.community.mapper.SysRoleMapper;
import com.blb.community.mapper.SysRoleMenuMapper;
import com.blb.community.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

    @Override
    public IPage<SysRole> getAllRoleList(RoleVO roleVO, IPage<SysRole> page) {
        return roleMapper.getAllRoleList(roleVO,page);
    }

    @Transactional
    @Override
    public void addRole(RoleMenuVO roleMenuVO) {
        roleMapper.insert(roleMenuVO);
//        roleMapper.addRole(roleMenuVO);
        Long roleId = roleMenuVO.getRoleId();

        List<Integer> menus = roleMenuVO.getMenus();

            for (int i = 0; i <menus.size() ; i++) {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(roleId);
                Integer integer = menus.get(i);
                long l = Long.parseLong("" + integer);
                sysRoleMenu.setMenuId(l);

                roleMenuMapper.addRolemenus(sysRoleMenu);
            }

    }

    @Override
    public void updataRoel(RoleMenuVO roleMenuVO) {
        roleMapper.updataRole(roleMenuVO);
    }

    /**
     * 角色假删
     * @param roleId
     */
    @Override
    public void delRole(Long roleId) {
        roleMapper.delRole(roleId);
    }

}
